(function (window, undefined) {
  /* 
    1,创建食物对象，w,h,backgroundColor, left, top, elements
    2,食物方法：render, 渲染到地图，同时位置随机；
    3，食物方法：remove, 被吃掉后删除；
    */
  const abso = "absolute";
  function Food(option) {
    //避免未传参或参数传递错误
    let ops = option instanceof Object ? option : {};
    this.width = ops.width || 20;
    this.height = ops.height || 20;
    this.x = ops.x || 0;
    this.y = ops.y || 0;
    this.backgroundColor = ops.backgroundColor || "red";
    //存放生成的元素
    this.elements = [];
  }
  //渲染
  Food.prototype.render = function (map) {
    // 创建一个div
    const div = document.createElement("div");
    //添加属性
    div.style.position = abso;
    div.style.width = this.width + "px";
    div.style.height = this.height + "px";
    div.style.left = this.width * Tools.getRandom(0, map.clientWidth / this.width - 1) + "px";
    div.style.top = this.height * Tools.getRandom(0, map.clientHeight / this.height - 1) + "px";
    div.style.backgroundColor = Tools.getColor();
    map.appendChild(div);
    //存放每次生成的新食物，被吃掉后删除
    this.elements.push(div);
  };
  //移出食物
  Food.prototype.remove = function (map, index) {
    //从dom中移除被吃掉的食物
    console.dir(map);
    map.removeChild(this.elements[index]);
    //同时存放的食物
    this.elements.splice(index, 1);
  };
  //暴露Food构造函数
  window.Food = Food;
})(window, undefined);
// const map = document.getElementById("map");
// var block = new Food();
// block.render(map);
